; Copyright (c) 2008, John Cronin
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;     * Redistributions of source code must retain the above copyright
;       notice, this list of conditions and the following disclaimer.
;     * Redistributions in binary form must reproduce the above copyright
;       notice, this list of conditions and the following disclaimer in the
;       documentation and/or other materials provided with the distribution.
;     * Neither the name of the copyright holder nor the
;       names of its contributors may be used to endorse or promote products
;       derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

MODULEALIGN		equ		1<<0		; align modules on page boundaries
MEMINFO			equ		1<<1		; provide memory map
FLAGS			equ		MODULEALIGN | MEMINFO
MAGIC			equ		0x1BADB002	; MultiBoot magic
CHECKSUM		equ		-(MAGIC + FLAGS)

STACKSIZE		equ		0x8000		; 32kB of stack space

HEADERLEN		equ		90

global		loader
extern		kmain
global		mb_addr

section	.multiboot
align	4
mbheader:
	dd		MAGIC
	dd		FLAGS
	dd		CHECKSUM
	
section .text
loader:
; store multiboot info
	mov		[mb_addr], ebx
	mov		[mb_magic], eax
	
; use our own gdt
	lgdt	[gdtinfo]
	jmp		0x8:.owngdt
	
.owngdt:
	mov		eax,	0x10
	mov		ss,		eax
	mov		ds,		eax
	mov		es,		eax
	mov		fs,		eax
	mov		gs,		eax
	
	mov		esp,	stack + STACKSIZE		; top of stack in bss section
	
disableints:
	mov		al, 0xff
	out		0x21, al
	out		0xa1, al
	cli
	
	push	dword [mb_addr]
	push	dword [mb_magic]
	call	kmain
	
	cli
	hlt
	
	
	
section .data
align 32
gdtinfo:
        dw 		gdt_end - gdt - 1
        dd 		gdt		; linear address	
	
	
	
section .gdt
align 32
gdt    dd      0,0     ; null
code    db      0xff, 0xff, 0x00, 0x00, 0x00, 10011010b, 11001111b, 0x00
data    db      0xff, 0xff, 0x00, 0x00, 0x00, 10010010b, 11001111b, 0x00
code3	db		0xff, 0xff, 0x00, 0x00, 0x00, 11111110b, 11001111b, 0x00
data3	db		0xff, 0xff, 0x00, 0x00, 0x00, 11110010b, 11001111b, 0x00
tss_ent	dd		0, 0
.end

times (2048 - .end + gdt) db 0
gdt_end:


section .bss
align 32
stack: resb STACKSIZE
mb_magic: resd	1
mb_addr: resd	1

